package com.example.comhnevcshenhaibing;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class shoujizhuce extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private Button back;
    private EditText eTtelephont;
    private Button bUtijiao;
    private String tel;
    private String pass;
    private String cfpass;
    private String tj;
    private EditText eTname;
    private EditText eTpassword;
    private String name;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shoujizhuce);
        initView();

        initEvent();
    }

    private void initDate(){
        tel = eTtelephont.getText().toString().trim();
        name = eTname.getText().toString().trim();
        pass = eTpassword.getText().toString().trim();

    }
    private void initEvent() {
        bUtijiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String telephone = eTtelephont.getText().toString().trim();
                String name = eTname.getText().toString().trim();
                String password = eTpassword.getText().toString().trim();

                if (TextUtils.isEmpty(password)){
                    Toast.makeText(shoujizhuce.this,"密码不能为空", Toast.LENGTH_SHORT).show();
                }else if (TextUtils.isEmpty(name)){
                    Toast.makeText(shoujizhuce.this, "用户名不能为空", Toast.LENGTH_SHORT).show();

                }else if (TextUtils.isEmpty(telephone)){
                    Toast.makeText(shoujizhuce.this, "手机号不能为空", Toast.LENGTH_SHORT).show();
                }else {
                    String url = "http://10.1.62.39/api/user/register";
                    JSONObject jsonObject = new JSONObject();
                    try {
                        jsonObject.put("telephone",telephone);
                        jsonObject.put("name","申海兵");
                        jsonObject.put("password",password);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    RequestQueue requestQueue = Volley.newRequestQueue(shoujizhuce.this);
                    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject jsonObject) {
                            Log.e(TAG,"onResponse:"+jsonObject.optString("msg"));
                            Toast.makeText(shoujizhuce.this, "注册成功", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(shoujizhuce.this,denglu.class);
                            startActivity(intent);
                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {
                            Toast.makeText(shoujizhuce.this,"网络异常",Toast.LENGTH_SHORT).show();
                        }
                    });

                    requestQueue.add(jsonObjectRequest);

                } }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(shoujizhuce.this,denglu.class);
                startActivity(intent);
            }
        });
    }
    private void initView() {
        back = findViewById(R.id.btn_back);
        eTtelephont = findViewById(R.id.et_telephone);
        eTname = findViewById(R.id.et_name);
        eTpassword = findViewById(R.id.et_password);
        bUtijiao = findViewById(R.id.btn_tijiao);

    }
}
